<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="LAY-list-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label  layui-label-jy">发运计划单</label>
                    <div class="layui-input-inline">
                        <input type="text" name="code" placeholder="请输入发运计划单号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label  layui-label-jy">发货仓库</label>
                    <div class="layui-input-inline">
                        <select lay-filter="warehouse_id_out" name="warehouse_id_out" lay-search>
                            <option value="">请选择发货仓库</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label  layui-label-jy">收货仓库</label>
                    <div class="layui-input-inline">
                        <select lay-filter="warehouse_id_rec" name="warehouse_id_rec" lay-search>
                            <option value="">请选择收货仓库</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">SKU</label>
                    <div class="layui-input-block">
                        <input type="text" name="sku" placeholder="" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button style="display:none" class="down" lay-submit="" lay-filter="LAY-list-down">下载</button>
                    <button type="reset" class="layui-btn layui-btn-primary reset" id="reset">重置</button>
                    <button class="layui-btn layuiadmin-btn-useradmin search" lay-submit lay-filter="LAY-list-search">搜索</button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-list"  lay-filter="LAY-list"></table>
            <!-- 行内工具 -->
            <script type="text/html" id="LAY-list-toolbar">
                {{# if(d.status==1){ }}
                <a class="layui-btn layui-btn-default layui-btn-xs" lay-event="confirm">确认</a>
                {{# } }}
            </script>
            <!-- 头部工具栏 -->
            <script type="text/html" id="toolbar">
                <div class="layui-btn-group fl layui-btn-group-jy">
                    <a lay-event="export" href="javascript:;" class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-download-circle">&nbsp;导出</a>
                    <a  lay-event="batchhandle" href="javascript:;" class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-add-circle-fine">&nbsp;批量确认</a>
                </div>
            </script>


        </div>
    </div>
</div>

<script>
    layui.use(['admin', 'table', 'form','toolJ','upload'], function () {
        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , toolJ = layui.toolJ
            , form = layui.form;
          //  ,laydate = layui.laydate;
           var warehouse = layui.data('yujiso').warehouse;
            var selectHtml = '';
            for (var i in warehouse) {
                selectHtml += '<option value="' + i + '">' + warehouse[i] + '</option>';
            }
            $("select[name='warehouse_id_rec']").append(selectHtml);
            $("select[name='warehouse_id_out']").append(selectHtml);
        form.render(null, 'LAY-list-form');
        table.render({
            elem: '#LAY-list'
            ,url: '/index/Countryonway/getList'
            ,toolbar: '#toolbar'
            ,cellMinWidth:100
            ,defaultToolbar: ['filter']
            ,cols: [[
                {type:'checkbox'}
                ,{field: 'code',title: '发运计划单号'}
                ,{field: 'local_order_no',title: '物流内单号'}
                ,{field: 'sku',title: 'SKU'}
                ,{field: 'unit_price', title: '出库单价（¥）'}
                ,{field: 'quantity', title: '发运数量'}
                ,{field: 'warehouse_str_out', title: '发货仓库'}
                ,{field: 'date', title: '发运时间'}
                ,{field: 'warequantity', title: '到货数量'}
                ,{field: 'warehouse_str_rec', title: '收货国家仓'}
                ,{field: 'waredate', title: '收货时间'}
                ,{field: 'differ_num', title: '差异数'}
                ,{field: 'differ_money', title: '差异金额（¥）'}
                ,{field: 'confirm_status', title: '状态'}
                ,{field: 'operator', title: '确认人'}
                ,{field: 'operate_time', title: '确认时间'}
                ,{title: '操作', width: 100, align:'center', fixed: 'right', toolbar: '#LAY-list-toolbar'}
            ]]
            ,page: true
            ,limit: 30
            ,text:{none: '暂无相关数据!' }
        });
        //监听行内操作
        table.on('tool(LAY-list)', function(obj){
            var data = obj.data;
            switch (obj.event) {
                case 'confirm':
                   var ids = []; // 选中的ID
                    ids.push(data.code+"_"+data.sku);
                    layer.confirm('你真的要确定这条数据？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        toolJ.ajax({"data":{"id":ids},"url":"/index/Countryonway/batchConfirm"},function (res) {
                            var icon = 1;
                            if (res.code != 1) icon = 2;
                            layer.msg(res.msg, {icon: icon});
                            $('.search').click();
                        });
                    });
                    break;
            }
        });
        //监听头部工具栏 操作
        table.on('toolbar(LAY-list)', function (obj) {
            //选中的数据
            var checkStatus = table.checkStatus('LAY-list')
                , data = checkStatus.data;
                checkValue=[];
                if(data){
                    for(var i in data){
                       checkValue.push({"code":data[i]['code'],"sku":data[i]['sku']});
                    }
                }
            //event对应模板的
            switch (obj.event) {
                case 'export':
                    $('.down').click();
                    break;
                case 'batchhandle':
                     var ids = []; // 选中的ID
                    var true_ids = [];
                    $.each(data,function (index,item) {
                        ids.push(item.code+"_"+item.sku);
                        if (item.status === 2) true_ids.push(item.code+"_"+item.sku);
                    });
                    if (ids.length <= 0){
                        layer.msg("请勾选需要确定的国家仓在途明细", {icon: 2});
                        return false;
                    }
                    true_ids = true_ids.join(); // 转成字符串
                    if (true_ids.length > 0){
                        layer.msg("国家仓在途明细"+true_ids+"已确定，请勿重复确定", {icon: 2});
                        return false;
                    }
                    layer.confirm('你真的要确定这些数据？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        toolJ.ajax({"data":{"id":ids},"url":"/index/Countryonway/batchConfirm"},function (res) {
                            var icon = 1;
                            if (res.code != 1) icon = 2;
                            layer.msg(res.msg, {icon: icon});
                            $('.search').click();
                        });
                    });
            }
        }); 
    });
</script>